/*
 * Project: Set Sample
 * Copyright (C) 2013 alf.labs gmail com,
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.alflabs.setsample.ui;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.alflabs.annotations.NonNull;
import com.alflabs.setsample.BuildConfig;
import com.alflabs.setsample.R;
import com.alflabs.setsample.model.SetSolution;
import com.alflabs.setsample.presenter.IAbstractView;
import com.alflabs.setsample.widgets.SetCheckButton;

import java.util.Set;

public class SetSolutionsFragment extends Fragment {

    private static final boolean DEBUG = BuildConfig.DEBUG;
    private static final String TAG = SetSolutionsFragment.class.getSimpleName();

    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);

        // Tell the framework to try to keep this fragment around during a configuration change.
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle state) {
        //noinspection UnnecessaryLocalVariable
        View root = inflater.inflate(R.layout.set_solutions_fragment, container, false);
        return root;
    }

    @Override
    public void onResume() {
        super.onResume();
        // Fragment is now visible to user and ready to interact. Make sure state is updated
        // according to the current board's state.

        FragmentManager fm = getFragmentManager();
        assert fm != null;
        Fragment fr = fm.findFragmentById(R.id.set_board_fragment);
        if (fr instanceof IAbstractView) {
            ((IAbstractView) fr).resetSolutions();
        }

    }

    void updateSolutions(@NonNull Set<SetSolution> displaySolutions,
                         @NonNull Set<SetSolution> compareSolutions) {
        View root = getView();
        if (root == null) return;

        ViewGroup table = (ViewGroup) root.findViewById(R.id.solutions);

        // TODO find a way to preserve existing sets when adding a new one.

        TextView tv = ((TextView) root.findViewById(R.id.set_counter));
        tv.setText(String.format("%d out of %d",
                Math.min(displaySolutions.size(), compareSolutions.size()),
                Math.max(displaySolutions.size(), compareSolutions.size())));

        // remove existing rows
        table.removeAllViewsInLayout();

        // create new ones
        //noinspection ConstantConditions
        LayoutInflater inflater = getActivity().getLayoutInflater();

        int index = 0;
        for (SetSolution s : displaySolutions) {
            View t = inflater.inflate(R.layout.set_solutions_3cards, table, true);
            assert t == table;

            int n = table.getChildCount();
            if (n < 2) continue;
            View row1 = table.getChildAt(n - 2);   // the cards
            View row2 = table.getChildAt(n - 1);   // the description
            assert row1 != null;
            assert row2 != null;

            SetCheckButton c1 = ((SetCheckButton) row1.findViewById(R.id.cell1));
            c1.setEnabled(false);
            c1.setCard(s.mCards[0]);
            SetCheckButton c2 = ((SetCheckButton) row1.findViewById(R.id.cell2));
            c2.setEnabled(false);
            c2.setCard(s.mCards[1]);
            SetCheckButton c3 = ((SetCheckButton) row1.findViewById(R.id.cell3));
            c3.setEnabled(false);
            c3.setCard(s.mCards[2]);

            String str = Integer.toString(++index);
            if (compareSolutions.contains(s)) {
                str += " \u2713";   // Unicode CHECK MARK
            }
            ((TextView) row1.findViewById(R.id.row_index)).setText(str);

            ((TextView) row2.findViewById(R.id.description)).setText(s.mDescription);
        }
    }
}
